package com.dataType.bag;

import java.util.Iterator;

public class Bag<Item> implements Iterable {


    BagNode first;

    private int size;

    public Bag(){

    }

    public void add(Item item){
        BagNode oldFist = first;
        first = new BagNode();
        first.item = item;
        first.next = oldFist;
        size++;
    }

    public int size(){
        return size;
    }

    public class BagNode<Item> {
        Item item;
        BagNode next;
    }

    @Override
    public Iterator iterator() {

        return new BagIterator();
    }

    private class BagIterator implements Iterator<Item>{

        BagNode node = first;

        @Override
        public boolean hasNext() {
            return node.next != null;
        }

        @Override
        public Item next() {
            Item item = (Item) node.item;
            node = node.next;
            return item;
        }
    }
}
